<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/* 建议如下：
data:执行成功返回的数据
status:简单的表示业务执行的状态，如成功success，登录失败，更新失败等
code:业务执行状态的代码（可不设计）
message:表示详细信息
version:版本号 
*/


class Team extends CI_Controller {


	var $_login_userid;
	
	private function htmlstr_loginbar(){
		$data=array();
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			$data['login_username']=$this->tank_auth->get_username();
		}else{
			$data['login_username']='';
		}
		$htmlstr=$this->load->view('htmlpage/loginbar',$data,true);
		return $htmlstr;
	}
	private function get_share_pagedata(){
//		$pagedata['head']=$this->load->view('htmlpage/head','',true);
//		$pagedata['logobar']=$this->load->view('htmlpage/logobar','',true);
		$pagedata['loginbar']=$this->htmlstr_loginbar();
//		$pagedata['navbar']=$this->load->view('htmlpage/navbar','',true);
//		$pagedata['footbar']=$this->load->view('htmlpage/footbar','',true);
		return $pagedata;
	}

	public function index($teamid=0){
		$this->load->database();
		if($this->_login_userid){
			$this->db->where('uid', $this->_login_userid);	
		}else{
			$this->db->where('uid', 5);
		}
		$this->db->where('tid', $teamid);
		$query = $this->db->get('team_user');
		$this->load->helper('url');
		$data = array();
		$data = $this->get_share_pagedata();
		
		if ($num_rows=$query->num_rows() > 0){
			$data['view'] = 'team';
			$data['teamId'] = $teamid;
			$this->load->view('home296',$data);
		}else{
			$data['message'] = '你不是团队的成员或你尚未登录，无权访问！';
			$this->load->view('message',$data);
		}
	}
	
	
	function __construct()
	 {	  
		parent::__construct();
		//$this->_login_userid=0;
		$this->_login_userid=$this->get_login_userid();
	 }

	private function get_login_userid(){
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			return $this->tank_auth->get_user_id();
		}else{
			return 0;
		}
	}
//检测当前登录用户是否是某个团队的成员
	private function check_is_member($teamid){
		$this->load->database();	
		$this->db->where('tid', $teamid);
		$query = $this->db->get('team_user');		
		if ($num_rows=$query->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
//将数组转换成json，并输出
	private function array_to_json($array){
		$json_string = json_encode($array);    
		echo $json_string; 
	}
	
	//取得所有参与的所有团队名称
	public function get_my_team_list(){
		$this->load->database();
		if($this->_login_userid){
			$this->db->where('uid', $this->_login_userid);
		}else{
			$this->db->where('uid', 5);
		}
		$query = $this->db->query("select team.* from team,team_user where team_user.tid=team.id AND team_user.uid=".($this->_login_userid?$this->_login_userid:'5')." ;");
		if ($num_rows=$query->num_rows() > 0)
		{
			$result_array['data']=$query->result_array();
			$this->array_to_json($result_array);
		}		
	}
	
//取得所有参与的所有团队成员
	public function get_team_member($tid){
		if($this->check_is_member($tid)){		
			$this->load->database();
			$query = $this->db->query("select users.* from team_user,users where team_user.uid=users.id and team_user.tid=".$tid.";");
			if ($num_rows=$query->num_rows() > 0)
			{
				$result_array['data']=$query->result_array();
				$this->array_to_json($result_array);
			}		
		}else{
			return;
		}
	}
	
	//取得指定团队的所有任务
	public function get_team_task($teamid=0,$rand=0,$workarea=0){
		$result_array = array();
		$r1= array();
		$r2 =array();
		$this->load->database();
		
//		$this->db->where('stage !=', 'finish');//默认只取得所有未完成的任务
		$this->db->where('archive !=', '1');//默认只取得所有未归档的任务
		$this->db->order_by('sort desc,parent asc');
		
		$query1 = $this->db->query("select task.* from task,team,team_user where task.tid=team.id and team_user.tid=team.id AND team.id=".$teamid." AND team_user.uid=".($this->_login_userid?$this->_login_userid:'5')." order by in_date desc;");
		
//		$query1 = $this->db->get('task');

		if ($num_rows=$query1->num_rows() > 0)
		{
//			$result_array['message']=$result_message;
			$result_array['data']=$query1->result_array();
			$result_array['status'] = 'success';
		}else{
			$result_array = array(
			   'status'=> 'nofound',
			   'message'=>'没有匹配的结果：'.$this->db->last_query()
		   );
		}

		$this->array_to_json($result_array);
	}
	
	
	
	public function insert(){	
//			$newnode_sort=time();
//			$newnode_sort=(microtime(true)*1000)%100000000000;
//			$newnode_sort= microtime(true);
			$newnode_sort= floor((microtime(true)-1400000000)*10);
//			$newnode_sort= (microtime(true)-1400000000)*10;
			$this->load->database();
			$data = array(
			   'teamname' => $_REQUEST['title'] ,
			   'in_date' => date("Y-m-d H:m:s"),
			   'uid' => $this->_login_userid
            );
			
			$this->db->insert('team', $data); 
			if($this->db->affected_rows()>0){
				$status=1;
				$newid=$this->db->insert_id();
				
				$data = array(
					'tid' => $newid ,
					'in_date' => date("Y-m-d H:m:s"),
					'uid' => $this->_login_userid
				);
			
				$this->db->insert('team_user', $data); 
				if($this->db->affected_rows()>0){
					$status=2;
				}
				
				
			}else{
				$status=0;
			}
			$result_arr = array(
				   'status'=> $status,
				   'message'=>'消息'.$this->db->last_query(),
				   'data' => array(
						'newid'=>$newid,
						'newnode_sort'=>$newnode_sort
				   )
				);
				
			if($this->_login_userid==0){
				$result_arr['status'] = 0;
				$result_arr['message'] = '你未登录或已经过期了，未成功新增团队，请先登录！';
			}		
			
		$json_string = json_encode($result_arr);    
		echo $json_string; 	
		
	}	
}
